/*******************************************************************************
 *
 * File Name:          StrutsFunctions.java
 *
 * Created:            2007-09-07 19:45:21
 *
 * Original Author:    yu.peng
 *
 *------------------------------------------------------------------------------
 * MODIFICATION HISTORY
 *------------------------------------------------------------------------------
 * WHEN                WHO                 WHY
 * 2007-09-07 19:45:21 yu.peng             new
 *------------------------------------------------------------------------------
 *
 ******************************************************************************/
/*******************/
/* PACKAGE SECTION */
/*******************/
package org.njo.webapp.root.taglib;

import java.util.Iterator;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.taglib.TagUtils;
import org.apache.struts.taglib.tiles.ComponentConstants;
import org.apache.struts.tiles.ComponentContext;
import org.njo.commons.lang.StringUtil;

public class StrutsFunctions {

    /**
     * 根据指定property的消息,取得request中的ActionMessage(Struts).
     * 
     * @param pageContext
     * @param property
     * @return
     */
    public static Iterator getActionMessages(PageContext pageContext,
            String property) {
        ActionMessages requestMessages = (ActionMessages) pageContext
                .getRequest().getAttribute(Globals.MESSAGE_KEY);
        if (requestMessages == null) {
            return null;
        }

        if (StringUtil.isEmptyOrSpaces(property)) {
            return requestMessages.get();
        } else {
            return requestMessages.get(property);
        }
    }

    /**
     * 从(Struts中的)MessageResources中,取得消息(资源).
     * 
     * @param pageContext
     * @param messageKey
     * @param args
     * @return
     */
    public static String getMessageResource(PageContext pageContext,
            String messageKey, Object[] args) {
        try {
            String message = TagUtils.getInstance().message(pageContext,
                    Globals.MESSAGES_KEY, Globals.LOCALE_KEY, messageKey, args);
            if (message==null) {
                return "---MsgKey---" + messageKey + "---";
            }
            return message;
        } catch (JspException e) {
            return e.toString();
        }
    }

    /**
     * 从(Struts中的)MessageResources中,取得消息(资源).
     * 
     * @param pageContext
     * @param messageKey
     * @param arg1
     * @return
     */
    public static String getMessageResource(PageContext pageContext,
            String messageKey) {
        return getMessageResource(pageContext, messageKey, null);
    }
    
    /**
     * 从(Struts中的)MessageResources中,取得消息(资源).
     * 
     * @param pageContext
     * @param messageKey
     * @param arg1
     * @return
     */
    public static String getMessageResource(PageContext pageContext,
            String messageKey, Object arg1) {
        Object[] args = new Object[] { arg1 };
        return getMessageResource(pageContext, messageKey, args);
    }

    /**
     * 从(Struts中的)MessageResources中,取得消息(资源).
     * 
     * @param pageContext
     * @param messageKey
     * @param arg1
     * @param arg2
     * @return
     */
    public static String getMessageResource(PageContext pageContext,
            String messageKey, Object arg1, Object arg2) {
        Object[] args = new Object[] { arg1, arg2 };
        return getMessageResource(pageContext, messageKey, args);
    }

    /**
     * 从(Struts中的)MessageResources中,取得消息(资源).
     * 
     * @param pageContext
     * @param messageKey
     * @param arg1
     * @param arg2
     * @param arg3
     * @return
     */
    public static String getMessageResource(PageContext pageContext,
            String messageKey, Object arg1, Object arg2, Object arg3) {
        Object[] args = new Object[] { arg1, arg2, arg3 };
        return getMessageResource(pageContext, messageKey, args);
    }

    /**
     * 从(Struts中的)MessageResources中,取得消息(资源).
     * 
     * @param pageContext
     * @param messageKey
     * @param arg1
     * @param arg2
     * @param arg3
     * @param arg4
     * @return
     */
    public static String getMessageResource(PageContext pageContext,
            String messageKey, Object arg1, Object arg2, Object arg3,
            Object arg4) {
        Object[] args = new Object[] { arg1, arg2, arg3, arg4 };
        return getMessageResource(pageContext, messageKey, args);
    }

    /**
     * 从(Struts中的)MessageResources中,取得消息(资源).
     * 
     * @param pageContext
     * @param messageKey
     * @param arg1
     * @param arg2
     * @param arg3
     * @param arg4
     * @param arg5
     * @return
     */
    public static String getMessageResource(PageContext pageContext,
            String messageKey, Object arg1, Object arg2, Object arg3,
            Object arg4, Object arg5) {
        Object[] args = new Object[] { arg1, arg2, arg3, arg4, arg5 };
        return getMessageResource(pageContext, messageKey, args);
    }

    /**
     * 取得在Tiles中定义的属性值.
     * 
     * @param pageContext
     * @param attribute
     * @return
     */
    public static Object getTilesAttribute(PageContext pageContext,
            String attribute) throws JspException {

        // Get context
        ComponentContext compContext = (ComponentContext) pageContext
                .getAttribute(ComponentConstants.COMPONENT_CONTEXT,
                        PageContext.REQUEST_SCOPE);

        if (compContext == null) {
            throw new JspException(
                    "Error - tag.getAsString : component context is not defined. Check tag syntax");
        }

        Object value = compContext.getAttribute(attribute);
        return value;

    }

}
